/*
 *  This file is part of Bracket Security
 *  Copyright 2011-2013 David R. Smith
 *
 */
package asia.redact.bracket.security.config;

import java.io.File;
import java.io.Serializable;

import asia.redact.bracket.properties.Properties;

/**
 * Implement a security configuration using a Properties back end
 * 
 * @author Dave
 *
 */
public class PropertiesConfig implements SecurityConfig, Serializable {

	private Properties props;
	private static final long serialVersionUID = 1L;

	public PropertiesConfig(Properties props) {
		super();
		this.props = props;
	}

	public String getKeypass() {
		test("keystore.keypass");
		return props.get("keystore.keypass");
	}

	public String getStorepass() {
		test("keystore.storepass");
		return props.get("keystore.storepass");
	}

	public String getKeystoreType() {
		test("keystore.type");
		return props.get("keystore.type");
	}

	public String getKeystoreFileExtension() {
		test("keystore.file.extension");
		return props.get("keystore.file.extension");
	}

	public String getSignatureAlgorithm() {
		test("signature.algorithm");
		return props.get("signature.algorithm");
	}

	public String getJCEProvider() {
		test("jce.provider");
		return props.get("jce.provider");
	}

	public String getSymmetricAlgorithm() {
		test("symmetric.algorithm");
		return props.get("symmetric.algorithm");
	}

	public int getSymmetricKeySize() {
		test("symmetric.key.size");
		return props.intValue("symmetric.key.size");
	}

	public int getAsymmetricKeySize() {
		test("asymmetric.key.size");
		return props.intValue("asymmetric.key.size");
	}

	public String getAsymmetricAlgorithm() {
		test("asymmetric.algorithm");
		return props.get("asymmetric.algorithm");
	}

	public boolean useCaching() {
		test("caching");
		return Boolean.valueOf(props.get("caching"));
	}

	public File getKeyMaterialsBase() {
		
		test("keymaterials.base");
		return new File(props.get("keymaterials.base"));
		
	}

	public String getSubjectDN() {
		test("cert.subjectDN");
		return props.get("cert.subjectDN");
	}

	public String getSerialNumber() {
		test("cert.serial");
		return props.get("cert.serial");
	}

	public String getAlias() {
		test("cert.alias");
		return props.get("cert.alias");
	}
	
	private void test(String key){
		if(!props.containsKey(key)){
			throw new RuntimeException("missing key: "+key+", which is required for the configuration");
		}
	}

}
